using System;
using System.Collections.Generic;
using System.Text;
using NHibernate;
using System.ComponentModel;
using Iesi.Collections.Generic;

namespace TanashCMS.Business
{
    [DataObject(true)]
    public class Product : CMSDataObject
    {
        private string mSearchKeywords = "";
        private int mCategoryID = 0;
        private int mWebsiteID = 0;
        private string mUPC;
        private double mPrice, mDiscount;
        private string mPhotoURL;
        private int mOrderIndex = 0;

        public virtual string SearchKeywords { get { return mSearchKeywords; } set { mSearchKeywords = value; } }
        public virtual int CategoryID { get { return mCategoryID; } set { mCategoryID = value; } }
        public virtual int WebsiteID { get { return mWebsiteID; } set { mWebsiteID = value; } }
        public virtual string UPC { get { return mUPC; } set { mUPC = value; } }
        public virtual double Price { get { return mPrice; } set { mPrice = value; } }
        public virtual double Discount { get { return mDiscount; } set { mDiscount = value; } }
        public virtual string PhotoURL { get { return mPhotoURL; } set { mPhotoURL = value; } }
        public virtual int OrderIndex { get { return mOrderIndex; } set { mOrderIndex = value; } }

        public static string generateUPC(int WebsiteID)
        {
            ISession session = Configuration.GetCurrentSession();
            object upc = session.CreateQuery("select max(p.UPC) from Product p where p.WebsiteID = " + WebsiteID.ToString()).UniqueResult();
            if (upc == null)
                upc = "1";

            string nextUPC="";

            // try to work with the value as a numeric
            try
            {
                int intUPC = int.Parse(nextUPC);
                intUPC++;
                nextUPC = intUPC.ToString();
            }
            catch/* (Exception ex)*/
            {
                // damn it's not numeric
                nextUPC = Guid.NewGuid().ToString().Replace("-", "").Substring(0,6);
            }
            return nextUPC.PadLeft(6, '0');
        }

        public static List<Product> List(int WebsiteID)
        {
            ISession session = Configuration.GetCurrentSession();
            List<Product> products = (List<Product>)Configuration.GetCurrentSession().CreateQuery("select p from Product p where p.WebsiteID = " + WebsiteID.ToString() + " and p.State = " + ((int)CMSObjectState.Published).ToString() + " order by p.Title").List<Product>();
            session.Flush();
            //session.Disconnect();
            return products;
        }

        public static List<Product> List(int WebsiteID, string SortData, int StartRow, int MaxRows)
        {
            ISession session = Configuration.GetCurrentSession();
            if (SortData == "")
                SortData = "Title";

            IQuery query = Configuration.GetCurrentSession().CreateQuery("select p from Product p where p.WebsiteID = " + WebsiteID.ToString() + " and p.State = " + ((int)CMSObjectState.Published).ToString() + " order by p." + SortData);
            if (StartRow != 0)
                query.SetFirstResult(StartRow);
            if (MaxRows != 0)
                query.SetMaxResults(MaxRows);

            List<Product> products = (List<Product>)query.List<Product>();

            session.Flush();
            //session.Disconnect();

            return products;
        }

        public static List<Product> List(int WebsiteID, int CategoryID, string OrderBy)
        {
            ISession session = Configuration.GetCurrentSession();
            if (string.IsNullOrEmpty(OrderBy)) OrderBy = "OrderIndex";
            List<Product> products = (List<Product>)session.CreateQuery("select p from Product p where p.CategoryID = " + CategoryID.ToString() + " and p.WebsiteID = " + WebsiteID.ToString() + " and p.State = " + ((int)CMSObjectState.Published).ToString() + " order by p." + OrderBy).List<Product>();
            session.Flush();
            //session.Disconnect();
            return products;
        }

        public static List<Product> List(int WebsiteID, int CategoryID)
        {
            return List(WebsiteID, CategoryID, null);
        }

        public static void Update(Product product)
        {
            ISession session = Configuration.GetCurrentSession();
            ITransaction tx = session.BeginTransaction();

            try
            {
                session.SaveOrUpdate(product);
                tx.Commit();
                session.Flush();
                session.Disconnect();
            }
            catch (Exception ex)
            {
                tx.Rollback();
                session.Disconnect();
                throw ex;
            }
        }

        public static void Insert(Product product)
        {
            ISession session = Configuration.GetCurrentSession();
            ITransaction tx = session.BeginTransaction();

            try
            {
                session.Save(product);
                tx.Commit();
                session.Flush();
                session.Disconnect();
            }
            catch (Exception ex)
            {
                tx.Rollback();
                session.Disconnect();
                throw ex;
            }
        }

        public static void Delete(Product product)
        {
            ISession session = Configuration.GetCurrentSession();
            ITransaction tx = session.BeginTransaction();

            try
            {
                session.Delete(product);
                tx.Commit();
                session.Flush();
                session.Disconnect();
            }
            catch (Exception ex)
            {
                tx.Rollback();
                session.Disconnect();
                throw ex;
            }
        }

        public static Product Get(int id)
        {
            return Configuration.GetCurrentSession().Get<Product>(id);
        }
    }

}
